package drds.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 使用joda解析date,可以得到date的year,month,day等字段值
 * 
 * 
 * 
 */
public class DateUtil {

	public static final String YYYY_MM_dd_HH_mm_ss = "YYYY-MM-dd HH:mm:ss";
	public static final String YYYY_MM_dd_HH_mm_ss_SSSSSS = "YYYY-MM-dd HH:mm:ss.SSSSSS";
	public static final String YYYY_MM_dd = "YYYY-MM-dd";
	public static final String HHH_mm_ss = "HHH:mm:ss";
	public static final String HHH_mm_ss_SSSSSS = "HHH:mm:ss.SSSSSS";

	/**
	 * 根据日期字符串解析得到date类型日期
	 */
	public static Date parseDate(String dateStr) throws ParseException {
		return parseDate(dateStr, YYYY_MM_dd_HH_mm_ss);
	}

	/**
	 * 根据日期字符串和日期格式解析得到date类型日期
	 */
	public static Date parseDate(String dateStr, String datePattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
		return sdf.parse(dateStr);
	}

	/**
	 * 获取date对象年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);

	}

	/**
	 * 获取date对象月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}

	/**
	 * 获取date对象天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取date对象小时数
	 * 
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取date对象分钟数
	 * 
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 获取date对象秒数
	 * 
	 * @param date
	 * @return
	 */
	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 获取date对象毫秒数
	 * 
	 * @param date
	 * @return
	 */
	public static int getMicroSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MILLISECOND);
	}

}